﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class ScreenPopUp
    {
        public int Add(Chain.Model.ScreenPopUp model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into ScreenPopUp(");
            builder.Append("CallerMemID,CallerMobile,CallerIsMem,CallerState,CallerDuration,CallerRemark,CallerCreateTime,CallerUserID,CallerShopID)");
            builder.Append(" values (");
            builder.Append("@CallerMemID,@CallerMobile,@CallerIsMem,@CallerState,@CallerDuration,@CallerRemark,@CallerCreateTime,@CallerUserID,@CallerShopID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CallerMemID", SqlDbType.Int, 4), new SqlParameter("@CallerMobile", SqlDbType.VarChar, 50), new SqlParameter("@CallerIsMem", SqlDbType.NVarChar, 50), new SqlParameter("@CallerState", SqlDbType.NVarChar, 50), new SqlParameter("@CallerDuration", SqlDbType.VarChar, 50), new SqlParameter("@CallerRemark", SqlDbType.VarChar, 500), new SqlParameter("@CallerCreateTime", SqlDbType.DateTime), new SqlParameter("@CallerUserID", SqlDbType.Int, 4), new SqlParameter("@CallerShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.CallerMemID;
            cmdParms[1].Value = model.CallerMobile;
            cmdParms[2].Value = model.CallerIsMem;
            cmdParms[3].Value = model.CallerState;
            cmdParms[4].Value = model.CallerDuration;
            cmdParms[5].Value = model.CallerRemark;
            cmdParms[6].Value = model.CallerCreateTime;
            cmdParms[7].Value = model.CallerUserID;
            cmdParms[8].Value = model.CallerShopID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int CallerID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from ScreenPopUp ");
            builder.Append(" where CallerID=@CallerID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CallerID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CallerID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string CallerIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from ScreenPopUp ");
            builder.Append(" where CallerID in (" + CallerIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int CallerID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from ScreenPopUp");
            builder.Append(" where CallerID=@CallerID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CallerID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CallerID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CallerID,CallerMemID,CallerMobile,CallerIsMem,CallerState,CallerDuration,CallerRemark,CallerCreateTime,CallerUserID,CallerShopID ");
            builder.Append(" FROM ScreenPopUp ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" CallerID,CallerMemID,CallerMobile,CallerIsMem,CallerState,CallerDuration,CallerRemark,CallerCreateTime,CallerUserID,CallerShopID ");
            builder.Append(" FROM ScreenPopUp ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.CallerID desc");
            }
            builder.Append(")AS Row, T.*  from ScreenPopUp T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("CallerID", "ScreenPopUp");
        }

        public Chain.Model.ScreenPopUp GetModel(int CallerID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 CallerID,CallerMemID,CallerMobile,CallerIsMem,CallerState,CallerDuration,CallerRemark,CallerCreateTime,CallerUserID,CallerShopID from ScreenPopUp ");
            builder.Append(" where CallerID=@CallerID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CallerID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CallerID;
            Chain.Model.ScreenPopUp up = new Chain.Model.ScreenPopUp();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["CallerID"] != null) && (set.Tables[0].Rows[0]["CallerID"].ToString() != ""))
            {
                up.CallerID = int.Parse(set.Tables[0].Rows[0]["CallerID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CallerMemID"] != null) && (set.Tables[0].Rows[0]["CallerMemID"].ToString() != ""))
            {
                up.CallerMemID = int.Parse(set.Tables[0].Rows[0]["CallerMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CallerMobile"] != null) && (set.Tables[0].Rows[0]["CallerMobile"].ToString() != ""))
            {
                up.CallerMobile = set.Tables[0].Rows[0]["CallerMobile"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CallerIsMem"] != null) && (set.Tables[0].Rows[0]["CallerIsMem"].ToString() != ""))
            {
                up.CallerIsMem = set.Tables[0].Rows[0]["CallerIsMem"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CallerState"] != null) && (set.Tables[0].Rows[0]["CallerState"].ToString() != ""))
            {
                up.CallerState = set.Tables[0].Rows[0]["CallerState"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CallerDuration"] != null) && (set.Tables[0].Rows[0]["CallerDuration"].ToString() != ""))
            {
                up.CallerDuration = set.Tables[0].Rows[0]["CallerDuration"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CallerRemark"] != null) && (set.Tables[0].Rows[0]["CallerRemark"].ToString() != ""))
            {
                up.CallerRemark = set.Tables[0].Rows[0]["CallerRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CallerCreateTime"] != null) && (set.Tables[0].Rows[0]["CallerCreateTime"].ToString() != ""))
            {
                up.CallerCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CallerCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CallerUserID"] != null) && (set.Tables[0].Rows[0]["CallerUserID"].ToString() != ""))
            {
                up.CallerUserID = int.Parse(set.Tables[0].Rows[0]["CallerUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CallerShopID"] != null) && (set.Tables[0].Rows[0]["CallerShopID"].ToString() != ""))
            {
                up.CallerShopID = int.Parse(set.Tables[0].Rows[0]["CallerShopID"].ToString());
            }
            return up;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM ScreenPopUp ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetScreenPopUpList(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "V_GetScreenPopUp";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "CallerID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public bool Update(Chain.Model.ScreenPopUp model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update ScreenPopUp set ");
            builder.Append("CallerMemID=@CallerMemID,");
            builder.Append("CallerMobile=@CallerMobile,");
            builder.Append("CallerIsMem=@CallerIsMem,");
            builder.Append("CallerState=@CallerState,");
            builder.Append("CallerDuration=@CallerDuration,");
            builder.Append("CallerRemark=@CallerRemark,");
            builder.Append("CallerCreateTime=@CallerCreateTime,");
            builder.Append("CallerUserID=@CallerUserID,");
            builder.Append("CallerShopID=@CallerShopID");
            builder.Append(" where CallerID=@CallerID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CallerMemID", SqlDbType.Int, 4), new SqlParameter("@CallerMobile", SqlDbType.VarChar, 50), new SqlParameter("@CallerIsMem", SqlDbType.NVarChar, 50), new SqlParameter("@CallerState", SqlDbType.NVarChar, 50), new SqlParameter("@CallerDuration", SqlDbType.VarChar, 50), new SqlParameter("@CallerRemark", SqlDbType.VarChar, 500), new SqlParameter("@CallerCreateTime", SqlDbType.DateTime), new SqlParameter("@CallerUserID", SqlDbType.Int, 4), new SqlParameter("@CallerShopID", SqlDbType.Int, 4), new SqlParameter("@CallerID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.CallerMemID;
            cmdParms[1].Value = model.CallerMobile;
            cmdParms[2].Value = model.CallerIsMem;
            cmdParms[3].Value = model.CallerState;
            cmdParms[4].Value = model.CallerDuration;
            cmdParms[5].Value = model.CallerRemark;
            cmdParms[6].Value = model.CallerCreateTime;
            cmdParms[7].Value = model.CallerUserID;
            cmdParms[8].Value = model.CallerShopID;
            cmdParms[9].Value = model.CallerID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

